﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.IO;
using System.Linq;
using System.Text;
using ReflectionStudio.Core;
using ReflectionStudio.Core.Events;
using KA41HLT;
using ReflectionStudio.Core.Helpers;
namespace WPFExcelReport
{

    public partial class KA41HLTData : BaseFunction
    {
        SETKA41HLT setData = new SETKA41HLT();
        public KA41HLTData()
            : base()
        {

        }
        public KA41HLTData(Dictionary<string, object> DicGlobalPara)
            : base(DicGlobalPara)
        {
        }
        public KA41HLTData(OracleConnection _myConnection, IntexReptServBLClient _proxy, string userID = "        ")
            : base(_myConnection, _proxy, userID)
        {
        }
        public DataSet LoadSetYarnPrice()
        {
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            try
            {
                string filePath = Path.Combine(PathHelper.ApplicationPath, @"Documents\HLTYARNPRICE.XML");
                if (System.IO.File.Exists(filePath))
                {
                    setResult.ReadXml(filePath);
                    setResult.Tables[0].TableName = "YARNPRICE";
                    setReturn.Merge(setResult);
                }
                else
                {
                    setResult = LoadYarnPrice();
                    setReturn.Merge(setResult);
                    SaveYarnPrice(setReturn.Tables["YARNPRICE"]);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return setReturn;
        }
        public void SaveYarnPrice(DataTable table)
        {
            DataSet SetReturn = new DataSet();
            try
            {
                SetReturn.Merge(table);
                string filePath = Path.Combine(PathHelper.ApplicationPath, @"Documents\HLTYARNPRICE.XML");
                if (System.IO.File.Exists(filePath))
                {
                    System.IO.File.Delete(filePath);
                }
                SetReturn.WriteXml(filePath);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public DataSet LoadSetProdCost()
        {
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            try
            {
                string filePath = Path.Combine(PathHelper.ApplicationPath, @"Documents\PRODCOST.XML");
                if (System.IO.File.Exists(filePath))
                {
                    setResult.ReadXml(filePath);
                    setResult.Tables[0].TableName = "PRODCOST";
                }
                setReturn.Merge(setResult);
                filePath = Path.Combine(PathHelper.ApplicationPath, @"Documents\PROCESSING.XML");
                setResult = new DataSet();
                if (System.IO.File.Exists(filePath))
                {
                    setResult.ReadXml(filePath);
                    setResult.Tables[0].TableName = "PROCESSING";
                }
                setReturn.Merge(setResult);

            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return setReturn;
        }
        public DataSet ReSetSETUP(string company, string user)
        {
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            DataSet settemp = new DataSet();
            try
            {
                ArrayList tables = new ArrayList();
                string fileroot = PathHelper.ApplicationPath + @"\Documents\";
                string filePath = fileroot + "HLTQUOTSETUP.xls";
                if (System.IO.File.Exists(filePath))
                {
                    if (exAgent == null)
                    {
                        exAgent = new ExcelX.ExcelLib.ExcelAgent(filePath);
                    }
                    setResult = exAgent.LoadDataTableDef(filePath, "");
                    foreach (DataTable tbTemp in setResult.Tables)
                    {
                        string tbName = tbTemp.TableName;
                        settemp = new DataSet();
                        settemp.DataSetName = "HLTQUOTSETUP";
                        settemp.Merge(tbTemp);
                        filePath = fileroot + tbName + ".XML";
                        if (System.IO.File.Exists(filePath))
                        {
                            System.IO.File.Delete(filePath);
                        }
                        settemp.WriteXml(filePath);
                    }
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

            return setReturn;
        }
        public void SaveSetProdCost(DataTable table)
        {
            DataSet SetReturn = new DataSet();

            try
            {
                SetReturn.Merge(table);
                string filePath = Path.Combine(PathHelper.ApplicationPath, "HLTPRODCOST.XML");
                if (System.IO.File.Exists(filePath))
                {
                    System.IO.File.Delete(filePath);
                }
                SetReturn.WriteXml(filePath);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public DataSet LoadYarnPrice()
        {
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            try
            {
                OracleCommand or1 = OracleDB.CreateCommand();
                or1.CommandText = "SELECT ROWNUM AS YID,'0' AS YSUPID,'HLT' AS YSUPNAME,GWBART AS YYARN,GWBVAR AS YVAR,GWBFARBE AS YCOLOR"
                    + ", GWPRVH*0.01 AS YPPRICE,GWPRAK*0.01 AS YCPRICE,GWPRKU*0.01 AS YFPRICE,'KG' AS YUOM, GWAENDDAT AS YEFFECTDATE,"
                    + "GWNEUBEN AS YUSER, GWNEUDAT AS YDATE,"
                    + "'' AS YREMARK1,'' AS YREMARK2,'' AS  YREMARK3"
                    + " FROM GWERT";
                setResult = Connect2Proxy(or1);
                if (setResult.Tables.Count > 0)
                {
                    setResult.Tables[0].TableName = "YARNPRICE";
                }
                setReturn.Merge(setResult);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return setReturn;
        }
        public DataSet LoadWAFARBEN(DataRow dwInput)
        {
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            try
            {
                OracleCommand or1 = OracleDB.CreateCommand();
                or1.CommandText = "SELECT DISTINCT WAFART,WAFVAR,WAFZUART,WAFZUSATZ,WAFCOLORIT,WAFSTATUS,WAFSFKZ,WAFBEZ"
               + " FROM INTEX.WAFARBEN WHERE  WAFART = :WAFART AND WAFVAR=:WAFVAR order by WAFZUSATZ desc";
                or1.Parameters.Add("WAFART", OracleType.NVarChar).Value = dwInput["WAART"].ToString();
                or1.Parameters.Add("WAFVAR", OracleType.NVarChar).Value = dwInput["WAVAR"].ToString();
                setResult = Connect2Proxy(or1);
                if (setResult.Tables.Count > 0)
                {
                    setResult.Tables[0].TableName = "WAFARBEN";
                }
                setReturn.Merge(setResult);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return setReturn;
        }
        public DataSet LoadWAART(string UsingID, string article = "")
        {
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            OracleCommand or1;
            try
            {
                UsingID = UsingID.Trim();
                UsingID = UsingID.Replace("\t", "");
                UsingID = Left(UsingID + "        ", 8);
                or1 = OracleDB.CreateCommand();
                or1.CommandText = "SELECT DISTINCT WAART, WAVAR"
               + " FROM INTEX.WASTAMM WHERE  rownum<200 and WAKUNDNR='00000000' AND WAART like '" + article + "%' AND warfertkz = 'E' AND WAFFERTKZ = 'E' "
                + " AND (WANEUB = '" + UsingID + "' OR '" + UsingID + "' IN ('FPDE    ','XHJUN   ')) order by 1 desc";
                setResult = Connect2Proxy(or1);
                if (setResult.Tables.Count > 0)
                {
                    setResult.Tables[0].TableName = "WAART";
                }
                setReturn.Merge(setResult);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return setReturn;
        }
        public DataSet GetData(DataRow dwInput)
        {
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            DataRow dwART = null;
            try
            {
                //KAAART,KAAVAR,KAAFARBE,KAABESCHI
                setReturn.Merge(dwInput.Table.Copy());
                setReturn.Tables[dwInput.Table.TableName].Rows.Add(dwInput.ItemArray);
                string Article = dwInput["KAAART"].ToString() + "-" + dwInput["KAAVAR"].ToString()
                    + "-" + dwInput["KAAFARBE"].ToString() + "-" + dwInput["KAABESCHI"].ToString();
                GreigeMasterData gd = new GreigeMasterData(OracleDB, proxy);
                setReturn = gd.LoadTableStructure(setReturn);
                EventDispatcher.Instance.RaiseStatus("开始品种:" + Article + "信息", StatusEventType.StartProgress);
                dwART = setData.WAFARBEN.NewWAFARBENRow();
                dwART["WAFART"] = dwInput["KAAART"];
                dwART["WAFVAR"] = dwInput["KAAVAR"];
                dwART["WAFZUSATZ"] = dwInput["KAAFARBE"];
                string color = dwInput["KAAFARBE"].ToString();
                string finish = dwInput["KAABESCHI"].ToString();

                setResult = gd.GetData(dwART, color, finish);

                setReturn.Merge(setResult);

                EventDispatcher.Instance.RaiseStatus("品种" + Article + "主数据已经读取", StatusEventType.RunningProgress);
                #region WarpMasterData
                WarpMasterData m_Wm = new WarpMasterData(OracleDB, proxy);
                if (setReturn.Tables.Contains("WASTKE"))
                {
                    foreach (DataRow dwWASTKE in setReturn.Tables["WASTKE"].Rows)
                    {
                        dwART = setData.BSFARB.NewBSFARBRow();
                        dwART["BSFKART"] = dwWASTKE["WAKKART"];
                        dwART["BSFKVAR"] = dwWASTKE["WAKKVAR"];
                        dwART["BSFKFARBE"] = dwWASTKE["WAKKFARBE"];
                        setResult = m_Wm.BuildWarpMasterData(dwART);
                        setReturn.Merge(setResult);
                        EventDispatcher.Instance.RaiseStatus("品种" + Article + "织轴信息已经读取", StatusEventType.RunningProgress);
                    }
                }
                #endregion

                #region YarnMasterData
                YarnMasterData m_YM = new YarnMasterData();
                m_YM.OracleDB = this.OracleDB;
                m_YM.proxy = this.proxy;
                setResult = new DataSet();
                bool flag = true;
                string YarnArt;
                string YarnVar;
                string YarnColor;
                DataRow dwGSCOLOR;
                DataTable tbGSCOLOR;
                tbGSCOLOR = setReturn.Tables["GSCOLOR"].Clone();
                while (flag)
                {
                    #region WASTGA
                    foreach (DataRow dwWASTGA in setReturn.Tables["WASTGA"].Rows)
                    {
                        //WAGGART              WAGGVAR WAGGFARBE  
                        YarnArt = dwWASTGA["WAGGART"].ToString();
                        YarnVar = dwWASTGA["WAGGVAR"].ToString();
                        YarnColor = m_YM.GetGB2312Value(dwWASTGA, "WAGGFARBE");
                        if (!m_YM.YarnExist(setReturn.Tables["GSDAT"], YarnArt, YarnVar))
                        {
                            if (!m_YM.YarnExist(tbGSCOLOR, YarnArt, YarnVar))
                            {
                                dwGSCOLOR = tbGSCOLOR.Rows.Add();
                                dwGSCOLOR["GSCART"] = YarnArt;
                                dwGSCOLOR["GSCVAR"] = YarnVar;
                                dwGSCOLOR["GSCFARBE"] = YarnColor;
                            }
                        }
                    }
                    #endregion

                    #region BASTGA
                    foreach (DataRow dwBASTGA in setReturn.Tables["BASTGA"].Rows)
                    {

                        //GSCOLOR/  GSCART               GSCVAR GSCFARBE  
                        YarnArt = dwBASTGA["BAGGART"].ToString();
                        YarnVar = dwBASTGA["BAGGVAR"].ToString();
                        YarnColor = m_YM.GetGB2312Value(dwBASTGA, "BAGGFARBE");
                        if (!m_YM.YarnExist(setReturn.Tables["GSDAT"], YarnArt, YarnVar))
                        {
                            if (!m_YM.YarnExist(tbGSCOLOR, YarnArt, YarnVar))
                            {
                                dwGSCOLOR = tbGSCOLOR.Rows.Add();
                                dwGSCOLOR["GSCART"] = YarnArt;
                                dwGSCOLOR["GSCVAR"] = YarnVar;
                                dwGSCOLOR["GSCFARBE"] = YarnColor;
                            }
                        }
                    }
                    #endregion

                    #region GASTGA
                    foreach (DataRow dwGASTGA in setReturn.Tables["GASTGA"].Rows)
                    {

                        YarnArt = dwGASTGA["GAGGART"].ToString();
                        YarnVar = dwGASTGA["GAGGVAR"].ToString();
                        YarnColor = dwGASTGA["GAGGFARBE"].ToString();
                        if (!m_YM.YarnExist(setReturn.Tables["GSDAT"], YarnArt, YarnVar))
                        {
                            if (!m_YM.YarnExist(tbGSCOLOR, YarnArt, YarnVar))
                            {
                                dwGSCOLOR = tbGSCOLOR.Rows.Add();
                                dwGSCOLOR["GSCART"] = YarnArt;
                                dwGSCOLOR["GSCVAR"] = YarnVar;
                                dwGSCOLOR["GSCFARBE"] = YarnColor;
                            }
                        }
                    }
                    #endregion
                    flag = false;
                    foreach (DataRow dwYan in tbGSCOLOR.Rows)
                    {
                        flag = true;
                        setResult = new DataSet();
                        setResult = m_YM.BuildYarnMasterData(dwYan);
                        setReturn.Merge(setResult);
                    }
                    tbGSCOLOR.Clear();

                }
                #endregion
                EventDispatcher.Instance.RaiseStatus("品种" + Article + " 已读取纱线信息", StatusEventType.RunningProgress);
                return setReturn;
                #region PatternMasterData
                PatternMasterData m_PD = new PatternMasterData(OracleDB, proxy);
                setResult = m_PD.LoadTableStructure(setResult);
                setReturn.Merge(setResult);
                DataTable dtPattern = null;
                if (setReturn.Tables.Contains("Pattern"))
                {
                    setReturn.Tables["Pattern"].Clear();
                }
                else
                {
                    setReturn.Tables.Add(tbPattern);
                }

                dtPattern = setReturn.Tables["Pattern"];
                DataRow dwPattern;

                #region WarpPattern
                foreach (DataRow dwTmp in setReturn.Tables["BSSATZ"].Rows)
                {
                    continue;
                    dwPattern = dtPattern.Rows.Add();
                    dwPattern["PatternTable"] = "WTSAER";
                    dwPattern["PatternType"] = "WarpPattern";
                    dwPattern["PatternKeyName"] = "WTBSRFNR";
                    dwPattern["PatternKey"] = dwTmp["BSSRFNR"].ToString().Trim();
                }
                #endregion

                #region SelvagePattern
                foreach (DataRow dwTmp in setReturn.Tables["WASTKE"].Rows)
                {
                    dwPattern = dtPattern.Rows.Add();
                    dwPattern["PatternTable"] = "WTLEIST";
                    dwPattern["PatternType"] = "SelvageLeftPattern";
                    dwPattern["PatternKeyName"] = "WTLLEIST";
                    dwPattern["PatternKey"] = dwTmp["WAKLLEIST"].ToString().Trim();
                    dwPattern = dtPattern.Rows.Add();
                    dwPattern["PatternTable"] = "WTLEIST";
                    dwPattern["PatternType"] = "SelvageRightPattern";
                    dwPattern["PatternKeyName"] = "WTLLEIST";
                    dwPattern["PatternKey"] = dwTmp["WAKRLEIST"].ToString().Trim();
                }
                #endregion

                #region WeaveRepeat /Denting
                foreach (DataRow dwTmp in setReturn.Tables["WASTAMM"].Rows)
                {
                    dwPattern = dtPattern.Rows.Add();
                    dwPattern["PatternTable"] = "SDDAT";
                    dwPattern["PatternType"] = "WeaveRepeat";
                    dwPattern["PatternKeyName"] = "SDKEY";
                    //select * from sddat where SDSA='0109' AND SDKEY Like '%4226%V'
                    dwPattern["PatternKey"] = dwTmp["WABIND"].ToString().Trim();

                    dwPattern = dtPattern.Rows.Add();
                    dwPattern["PatternTable"] = "WTEINZ";
                    dwPattern["PatternType"] = "Denting";
                    dwPattern["PatternKeyName"] = "WTEEINZNR";
                    dwPattern["PatternKey"] = dwTmp["WAEINZNR"].ToString().Trim();

                }
                #endregion

                #region WeftPattern
                foreach (DataRow dwTmp in setReturn.Tables["WTGEW"].Rows)
                {
                    dwPattern = dtPattern.Rows.Add();
                    dwPattern["PatternTable"] = "WTSCHUSS";
                    dwPattern["PatternType"] = "WeftPattern";
                    dwPattern["PatternKeyName"] = "WTSSFLGNR";
                    dwPattern["PatternKey"] = dwTmp["WTGSFLG"].ToString().Trim();

                }
                #endregion

                #region DraftingNumber

                foreach (DataRow dwTmp in setReturn.Tables["WASTPASS"].Rows)
                {
                    dwPattern = dtPattern.Rows.Add();
                    dwPattern["PatternTable"] = "WTPASS";
                    dwPattern["PatternType"] = "DraftingNumber";
                    dwPattern["PatternKeyName"] = "WTPPASSNR";
                    dwPattern["PatternKey"] = dwTmp["WSPPASS"].ToString().Trim();

                }
                #endregion
                foreach (DataRow dwPatterns in dtPattern.Rows)
                {
                    setResult = m_PD.BuildPatternMasterData(setResult, dwPatterns);
                    EventDispatcher.Instance.RaiseStatus("品种" + Article + dwPattern["PatternTable"].ToString() + " 已读花型线信息", StatusEventType.RunningProgress);
                    setReturn.Merge(setResult);
                }
                #endregion
            }
            catch (Exception ex)
            {
                Tracer.Error(ABPOS + ex.StackTrace, ex);
            }
            finally
            {
                EventDispatcher.Instance.RaiseStatus("完成已读取纱线信息", StatusEventType.StopProgress);
            }
            return setReturn;
        }
        public DataSet BuildEKPR(DataRow dwUI)
        {
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            DataTable tableLocal = null;
            try
            {
                OracleCommand or1 = or1 = OracleDB.CreateCommand();

                or1.CommandText = " select a.EKPART,a.EKPVAR,a.EKPFARBE, a.price, a.lastdat "
                    + " from last_ek_pr a ";
                setResult = Connect2Proxy(or1);
                if (setResult.Tables.Count > 0)
                {
                    tableLocal = setResult.Tables[0];
                    tableLocal.TableName = "LASTEKPR";
                }
                setReturn.Merge(setResult);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return setReturn;
        }
    }
}
